// signin.js
// Implement 'stay signed in'.

function StaySignedIn(changeInfo)
{
	if(changeInfo.removed)
		return;
	var cookie = changeInfo.cookie;
	var a_year = 365 * 24 * 60 * 60;
	var now = (new Date()).getTime()/1000;
	if(cookie.name == "56f61_winduser" && 
		cookie.expirationDate - now < a_year)
	{
		var newCookie = ({
			url: (cookie.secure ? "https" : "http") + "://" + 
				cookie.domain + cookie.path, 
			name: cookie.name, 
			value: cookie.value, 
			path: cookie.path, 
			secure: cookie.secure, 
			httpOnly: cookie.httpOnly, 
			expirationDate: now + a_year * 10, 
			storeId: cookie.storeId
		});
		if(!cookie.hostOnly)
			newCookie.domain = cookie.domain;
		chrome.cookies.set(newCookie);
	}
}

//The following code works if and only if onChanged won't be fired unless 
//the value of StaySignedIn REALLY changes and calling removeListener which 
//the event doesn't have won't raise an error.
Options.SignIn.onChanged = (function()
{
	if(Options.SignIn.StaySignedIn)
		chrome.cookies.onChanged.addListener(StaySignedIn);
	else
		chrome.cookies.onChanged.removeListener(StaySignedIn);
});
Options.SignIn.onChanged();


